package com.wanglibao.woodtrade.woodtrade.ui.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.wanglibao.woodtrade.woodtrade.R;
import com.wanglibao.woodtrade.woodtrade.config.NetConfig;
import com.wanglibao.woodtrade.woodtrade.entity.GoodsListDetails;
import com.wanglibao.woodtrade.woodtrade.jsonrpc.JsonRPCAsyncTask;
import com.wanglibao.woodtrade.woodtrade.model.impl.UserImpl;
import com.wanglibao.woodtrade.woodtrade.ui.activity.adapter.ItemLvMyCommodityTwoActivityAdapter;
import com.wanglibao.woodtrade.woodtrade.util.ToastUtil;
import com.wanglibao.woodtrade.woodtrade.widget.SVListView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
/*我的商品的二级*/

public class MyCommodityTwoActivity extends BaseActivity {

    @Bind(R.id.lv_my_commodity_activity)
    SVListView mLvMyCommodityActivityTwo;
    @Bind(R.id.tv_pinlei)
    TextView tvPinlei;
    @Bind(R.id.tv_kuncunliang)
    TextView tvKuncunliang;
    @Bind(R.id.tv_yishangjia)
    TextView tvYishangjia;
    @Bind(R.id.tv_cangwei)
    TextView tvCangwei;
    @Bind(R.id.bt_add_shanpin)
    Button btAddShanpin;
    //=========================================
    GoodsListDetails mGoodslistDetails;
    List<GoodsListDetails.ListBean> mListBean;
    ItemLvMyCommodityTwoActivityAdapter mAdapter;
    String Category_id;
    String Goods_id;
    String Dopt;
    String Unit;
    String Place;
    Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.arg2 != 2) {
                try{
                    mGoodslistDetails = new Gson().fromJson(msg.obj.toString(), new TypeToken<GoodsListDetails>(){}.getType());
                }catch (Exception e){
                    mGoodslistDetails = new GoodsListDetails();
                    mGoodslistDetails.setGoodsInfo(new GoodsListDetails.GoodsInfoBean());
                    mGoodslistDetails.setList(new ArrayList<GoodsListDetails.ListBean>());
                    try {
                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        mGoodslistDetails.setGoodsInfo(new Gson().fromJson(jsonObject.getString("goodsInfo"),mGoodslistDetails.getGoodsInfo().getClass()));
                    } catch (JSONException e1) {
                        e1.printStackTrace();
                    }
                }

                mListBean = mGoodslistDetails.getList();
                mAdapter = new ItemLvMyCommodityTwoActivityAdapter(MyCommodityTwoActivity.this, mListBean);
                mLvMyCommodityActivityTwo.setAdapter(mAdapter);
                initView();
            }
            return false;
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_commodity_two);
        ButterKnife.bind(this);
        if (Build.VERSION.SDK_INT >= 19) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        } else {
            rl_common_title.setPadding(0, 0, 0, 0);
        }
        initData();
    }


    @Override
    protected void onResume() {
        super.onResume();
        NetWorking();
    }

    private void initData() {
        Category_id = getIntent().getStringExtra("category_id");
        Goods_id = getIntent().getStringExtra("Goods_id");
        Dopt = getIntent().getStringExtra("dopt");
        tvTitle.setText("我的商品");
        addViewClicklistener(tvBack, btAddShanpin);
    }

    private void initView() {
        Unit=mGoodslistDetails.getGoodsInfo().getUnit_name();
        Place=mGoodslistDetails.getGoodsInfo().getPlace();
        tvPinlei.setText(mGoodslistDetails.getGoodsInfo().getCategory_name());
        tvKuncunliang.setText(mGoodslistDetails.getGoodsInfo().getQuantity()+mGoodslistDetails.getGoodsInfo().getUnit_name());
        tvYishangjia.setText(mGoodslistDetails.getGoodsInfo().getQuanti()+mGoodslistDetails.getGoodsInfo().getUnit_name());
        tvCangwei.setText(mGoodslistDetails.getGoodsInfo().getPosition());
    }

    public void NetWorking() {
        JsonRPCAsyncTask jsonRPCAsyncTask = new JsonRPCAsyncTask(this, NetConfig.SERVER + NetConfig.TYPE_USER,
                NetConfig.userGoodsListsDetail, mHandler,
                "jsonObject", NetConfig.TaskId.userGoodsListsDetail, new Object[]{new UserImpl(this).getUser().token, Goods_id});
        jsonRPCAsyncTask.execute(new Void[]{});
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.imgbtn_commonTitle_back:
                finish();
                break;
            case R.id.bt_add_shanpin:
                Intent intent = new Intent(MyCommodityTwoActivity.this, EditCommodityActivity.class);
                intent.putExtra("woodType", tvPinlei.getText().toString());
                intent.putExtra("Goods_id", Goods_id);
                intent.putExtra("dopt", Dopt);
                intent.putExtra("unit",Unit);
                if(!Place.equals("")){
                    intent.putExtra("place",Place);
                    startActivity(intent);
                }else {
                    ToastUtil.createNormalToast("产地字段为空！");
                }
                break;
            default:
                break;
        }
    }
}
